package cm309.BookingSystem.common;

import java.io.IOException;
import java.io.StringWriter;
import java.util.regex.Pattern;

import org.jboss.logging.Logger;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;

public  class Common {
	
	public static final Logger logger = Logger.getLogger("com.tricubes"); 
	public static String beanToJson(Object obj) throws IOException {  
	    
	        ObjectMapper mapper = new ObjectMapper(); 
	        StringWriter writer = new StringWriter();  
	        JsonGenerator gen = new JsonFactory().createJsonGenerator(writer);  
	        mapper.writeValue(gen, obj);  
	        gen.close();  
	        String json = writer.toString();  
	        writer.close();  
	        return json;  
	    }  
	

	public static ResponseEntity<String> createJsonResponse( Object o )
    {
        HttpHeaders headers = new HttpHeaders();
        headers.set(  "Content-Type", "application/json" );
        String json;
		try {
			json = beanToJson( o );
		      return new ResponseEntity<String>( json, headers, HttpStatus.CREATED );

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
		return new ResponseEntity<String>( "[]", headers, HttpStatus.CREATED );
    }
	
	public static boolean isNumeric(String str){ 
	    Pattern pattern = Pattern.compile("[0-9]*"); 
	    return pattern.matcher(str).matches();    
	 } 

}
